using System;
using System.Collections.Generic;
using System.Text;
using Northwoods.Go;
using Northwoods.Go.Xml;

namespace Rules.Diagram
{
    public class TransformRuleLink : GoXmlTransformer
    {
        public TransformRuleLink()
        {
            this.TransformerType = typeof(RuleLink);
            this.ElementName = "link";
        }

        public override void GenerateAttributes(Object obj)
        {
            base.GenerateAttributes(obj);
            RuleLink link = (RuleLink)obj;
            RuleNode from = link.FromNode as RuleNode;
            if(from==null)
                return;
            if (from != null)
            {
                String fromid = this.Writer.FindShared(from);
                WriteAttrVal("from", fromid);
            }
            RuleNode to = link.ToNode as RuleNode;
            if(to==null)
                return;
            if (to != null)
            {
                String toid = this.Writer.FindShared(to);
                WriteAttrVal("to", toid);
            }
            WriteAttrVal("points", link.RealLink.CopyPointsArray());
            if (link.FromPort == from.LeftPort)
            {
                WriteAttrVal("fromside", "left");
            }
            else if (link.FromPort == from.RightPort)
            {
                WriteAttrVal("fromside", "right");
            }
            if (link.ToPort == to.LeftPort)
            {
                WriteAttrVal("toside", "left");
            }
            else if (link.ToPort == to.RightPort)
            {
                WriteAttrVal("toside", "right");
            }
            GoText lab = link.FromLabel as GoText;
            if (lab != null)
            {
                WriteAttrVal("label", lab.Text);
            }
        }

        public override void ConsumeAttributes(Object obj)
        {
            base.ConsumeAttributes(obj);
            RuleLink link = (RuleLink)obj;
            String fromid = StringAttr("from", null);
            if (fromid != null)
            {
                RuleNode from = this.Reader.FindShared(fromid) as RuleNode;
                if (from != null)
                {
                    // assume links come out from the bottom port, unless otherwise specified
                    String fromside = StringAttr("fromside", "bottom");
                    if (fromside == "left")
                        link.FromPort = from.LeftPort;
                    else if (fromside == "right")
                        link.FromPort = from.RightPort;
                    else
                        link.FromPort = from.BottomPort;
                }
            }
            String toid = StringAttr("to", null);
            if (toid != null)
            {
                RuleNode to = this.Reader.FindShared(toid) as RuleNode;
                if (to != null)
                {
                    // assume links go into the top port, unless otherwise specified
                    String toside = StringAttr("toside", "top");
                    if (toside == "left")
                        link.ToPort = to.LeftPort;
                    else if (toside == "right")
                        link.ToPort = to.RightPort;
                    else
                        link.ToPort = to.TopPort;
                }
            }
            if (IsAttrPresent("points"))
                link.RealLink.SetPoints(PointFArrayAttr("points", null));
            if (IsAttrPresent("label"))
            {
                GoText lab = new GoText();
                lab.Selectable = false;
                lab.Text = StringAttr("label", lab.Text);
                link.FromLabel = lab;
                link.LayoutChildren(null);
            }
        }
    }
}
